package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.SListViewSlideAdapter;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MineRecipients extends BaseActivity implements View.OnClickListener{

    private ListView servicerlist;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private List<HashMap<String,String>>linkList;
    private HashMap<String,String>map;
    private JSONArray array;
    private String url = "http://192.168.3.77:8088/YiViOnlineServer";
    private ImageView linkrecipentsback;
    private Button recipientsadd;
    private TextView delate;
    private TextView delete;
    private TextView edit;
    private String is = "1";

    private int ADDRESS = 1002;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_recipients);
        recipientsadd = (Button) findViewById(R.id.recipients_addbt);
        servicerlist = (ListView)findViewById(R.id.servicerlist);
        linkrecipentsback = (ImageView) findViewById(R.id.link_recipents_back);
       /* delete = (TextView) findViewById(R.id.servcer_delete);
        edit = (TextView) findViewById(R.id.servcer_Edit);*/

        sharedPreferences =getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        linkList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                getsLinkdata();
            }
        }).start();




        linkrecipentsback.setOnClickListener(this);
        recipientsadd.setOnClickListener(this);
        /*delate.setOnClickListener(this);
        edit.setOnClickListener(this);*/
    }

    private List getsLinkdata() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/contact/getContactList");
        params.addHeader("Content-Type","application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("type","1");
        params.addParameter("params",j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                com.alibaba.fastjson.JSONObject obj = JSON.parseObject(result);
                String data = obj.getString("data");
                array = JSONArray.parseArray(data);

                for (int i = 0; i < array.size(); i++) {
                    map = new HashMap();
                    com.alibaba.fastjson.JSONObject obj1 = JSON.parseObject(String.valueOf(array.get(i)));
                    String name = obj1.getString("name");
                    String phone = obj1.getString("phone");
                    String address = obj1.getString("address");
                    map.put("name",name);
                    map.put("phone",phone);
                    map.put("address",address);
                    linkList.add(map);
                    // 设置listview
                    final SListViewSlideAdapter slideAdapter = new SListViewSlideAdapter(MineRecipients.this,linkList);
                    servicerlist.setAdapter(slideAdapter);
                    servicerlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            HashMap<String,String> item = (HashMap<String, String>) slideAdapter.getItem(position);
                            String address1 = item.get("address");
                            String phone1 = item.get("phone");
                            String name1 = item.get("name");
                            Intent i = new Intent(MineRecipients.this,OnceSendActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putString("address1",address1);
                            bundle.putString("phone1",phone1);
                            bundle.putString("name1",name1);
                           i.putExtras(bundle);
                            setResult(RESULT_OK,i);
                            finish();
                        }
                    });

                    slideAdapter.setOnClickListenerEditOrDelete(new SListViewSlideAdapter.OnClickListenerEditOrDelete() {
                        @Override
                        public void OnClickListenerEdit(int position) {
                            //编辑
                            Intent intent  = new Intent(MineRecipients.this,AddLinkmanActivity.class);
                            HashMap<String, String> bianji = linkList.get(position);
                            String name1 = bianji.get("name");
                            String phone1 = bianji.get("phone");
                            String address1 = bianji.get("address");
                            intent.putExtra("name1",name1);
                            intent.putExtra("phone1",phone1);
                            intent.putExtra("address1",address1);
                            intent.putExtra("is",is);
                            startActivity(intent);
                            linkList.remove(position);
                        }

                        @Override
                        public void OnClickListenerDelete(int position) {
                            // 删除
                            linkList.remove(position);
                            slideAdapter.notifyDataSetChanged();

                        }
                    });
                    slideAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        return linkList;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.link_recipents_back:
                finish();
                break;
            case R.id.recipients_addbt:
               Intent intent = new Intent(MineRecipients.this,AddLinkmanActivity.class);
                intent.putExtra("is", is);
                startActivity(intent);
                break;

        }
    }
}
